Ext.define('Jee.view.Login',{
extend:'Ext.window.Window',
alias: 'widget.loginForm',
requires: ['Ext.form.*'],
 initComponent:function(){ 
     var form = Ext.widget('form',{
     border: false,
     bodyPadding: 10,
     fieldDefaults: { 
                    labelAlign: 'left',
                    labelWidth: 55,
                    labelStyle: 'font-weight:bold'},
    defaults: {margins: '0 0 10 0'},
    items:[{xtype: 'textfield',
		    fieldLabel: '用户名',
		    blankText : '用户名不能为空',
		    name:'userName',
		    id:'userName',
		    value:'admin',
		    allowBlank: false,
		    width:240},
		    
            {xtype: 'textfield',
            fieldLabel: '密   码',
            allowBlank: false,
            blankText : '密码不能为空',
            name:'password',
            id:'password',
            value:'123456',
            width:240,
            listeners : {  
		        specialKey  : function(f,e) {
		            if (e.getKey() == Ext.EventObject.ENTER) {  
		               form.login(); 
		            }  
		        }  
		     } , 
            inputType : 'password'
     }],
     
    buttons:[{text:'登陆',
             handler:function(){
             form.login();
    }}],
    login:function(){
    	   var form = this.getForm();
           var win = this.up('window');
           if(form.isValid()){
        	    var userName = form.findField('userName').getValue();
        	    var password = form.findField('password').getValue();
        	    var data ={};
        	    data.userName = userName;
        	    data.password = password;
        	    var request = {};
        	    request.serviceId="01_02_02_11";
        	    request.data = data;
        	    var progressBar =  Ext.Msg.wait("正在登录......","请稍候");
                Ext.Ajax.request({  
					url : 'json/',  
					method : 'POST', 
					//params : Ext.encode(request),  
					jsonData:request,
					timeout : 60000,  
					success : function(form, action) {
                	       progressBar.hide();
                	       var result = Ext.decode(form.responseText);
                           if(result.successFlag){
                        	    var records = result.data.menus;
                        	    var bbarData =[];
                        	    var firstTreePanel;
					    		for(var i=0;i<records.length;i++){
					    			var id = records[i].id;
					    			var text = records[i].text;
					    			bbarData.push({
					    				pressed:false,
					    				text:text,
					    				id:id,
					    				iconCls: 'tabs',
					    				handler: function(){
					    				    var targetPanel = BA.leftModules.get(this.id);
					    				    if(targetPanel){
					    				    	 Ext.getCmp('left').activeLeftPanel(targetPanel);
					    				    }
					    				}
					    			});
					    			if(i != records.length-1){
					    				bbarData.push({xtype:'tbseparator'});
					    			}

					    			var children = records[i].children;
					    			var treePanel = Ext.create('Ext.tree.Panel', {
								         title: text,
								         width: 200,
								         checked:false,
								         store: Ext.create('Ext.data.TreeStore',{
								        	 model:'MenuModel',
								        	 root: {
									              expanded: true,
									              text: text,
									              children: children
									         }
								         }),
								         rootVisible: false
					               });
					    		   treePanel.on("itemclick", function(view,record,item,index,e) {
						    		  var mainPanel = Ext.getCmp('main');   
						    		  var id = record.get('id');   	   
						    		  var tab = mainPanel.getComponent(id);
							    	  if(tab){
							    		  mainPanel.setActiveTab(tab);
							    	  }else{
                                           var viewClass = record.raw.viewName;
						    			   if(viewClass){
						    				   panel = Ext.create(viewClass,{
						    					   id:id,
							    				   title:record.get('text'),
							    				   iconCls:'tabs',
							    				   closable: true
						    					   });
						    				    
						    				    var p = mainPanel.add(panel);
							    		        mainPanel.setActiveTab(p);
						    				}else{
						    					 Ext.MessageBox.show({
					                    		   width:150,
					                    		   title:"错误",
					                    		   buttons: Ext.MessageBox.OK,
					                    		   msg:"没有找到对应的视图"
				                    		   })
						    				} 
							    		 
							    	  }
					               });
					    		   Ext.getCmp('leftMenuPanel').add(treePanel);
					    		   if(i==0){
					    				 firstTreePanel = treePanel;
					    		    }	 
					    			BA.leftModules.add(id,treePanel);	
					    		}
					    		var bbar=new Ext.Toolbar({dock:'bottom',items:bbarData});
					    		Ext.getCmp('header').addDocked(bbar,'bottom');

					    		if(firstTreePanel){
					    			Ext.getCmp('leftMenuPanel').getLayout().setActiveItem(firstTreePanel);
					    		    Ext.getCmp('leftMenuPanel').doLayout();
					    		}
                        	  win.hide();
                           }else{
                        	    progressBar.hide();
                        	    Ext.MessageBox.show({
	                    		   width:150,
	                    		   title:"登陆失败",
	                    		   buttons: Ext.MessageBox.OK,
	                    		   msg:result.msg
                    		   })
                           }  
					},
					failure : function(form, action) { 
                    	  Ext.MessageBox.show({
	                    		   width:150,
	                    		   title:"登陆失败",
	                    		   buttons: Ext.MessageBox.OK,
	                    		   msg:'登陆失败'
                    	  })
					}
                });
           }
	}
   });
  
  Ext.apply(this,{height: 160,
                   width: 280,
                   title: '用户登陆',
                   closeAction: 'hide',
                   closable : false,
                   iconCls: 'win',
                   layout: 'fit',
                   modal : true,
                   plain : true,
                   resizable: false,
                   items:form});
  this.callParent(arguments); 
 }});   

